﻿using System.Web.Mvc;
using System.Web.Routing;
using CodeContrib.Web.Mvc.Routing;

namespace CodeContribBlog.Web.Mvc
{
	public static class RouteConfig
	{
		private static void MapPostRoutes(this RouteCollection routes)
		{
			routes.MapTenantRoute("PostsByTag", "{tenant}/tag/{slug}", new
				                                                           {
					                                                           tenant = "default",
					                                                           controller = "Post",
					                                                           action = "Tag"
				                                                           }, new
					                                                              {
						                                                              tenant = new TenantRouteConstraint()
					                                                              }, new[]
						                                                                 {
							                                                                 "CodeContribBlog.Web.Mvc.Controllers"
						                                                                 });

			routes.MapTenantRoute("PostsByYearMonth", "{tenant}/archive/{year}/{month}", new
				                                                                             {
					                                                                             tenant = "default",
					                                                                             controller = "Post",
					                                                                             action = "Archive"
				                                                                             }, new
					                                                                                {
						                                                                                year = new PositiveIntegerRouteConstraint(),
						                                                                                month = new PositiveIntegerRouteConstraint(),
						                                                                                tenant = new TenantRouteConstraint()
					                                                                                }, new[]
						                                                                                   {
							                                                                                   "CodeContribBlog.Web.Mvc.Controllers"
						                                                                                   });

			routes.MapTenantRoute("PostsByYear", "{tenant}/archive/{year}", new
				                                                                {
					                                                                tenant = "default",
					                                                                controller = "Post",
					                                                                action = "Archive"
				                                                                }, new
					                                                                   {
						                                                                   year = new PositiveIntegerRouteConstraint(),
						                                                                   tenant = new TenantRouteConstraint()
					                                                                   }, new[]
						                                                                      {
							                                                                      "CodeContribBlog.Web.Mvc.Controllers"
						                                                                      });

			routes.MapTenantRoute("PostDetails", "{tenant}/{id}/{slug}", new
				                                                             {
					                                                             tenant = "default",
					                                                             controller = "Post",
					                                                             action = "Details",
					                                                             slug = UrlParameter.Optional
				                                                             }, new
					                                                                {
						                                                                id = new PositiveIntegerRouteConstraint(),
						                                                                tenant = new TenantRouteConstraint()
					                                                                }, new[]
						                                                                   {
							                                                                   "CodeContribBlog.Web.Mvc.Controllers"
						                                                                   });
		}

		private static void MapSearchRoutes(this RouteCollection routes)
		{
			routes.MapTenantRoute("Search", "{tenant}/search", new
				                                                   {
					                                                   tenant = "default",
					                                                   controller = "Search",
					                                                   action = "Index"
				                                                   }, new
					                                                      {
						                                                      tenant = new TenantRouteConstraint()
					                                                      }, new[]
						                                                         {
							                                                         "CodeContribBlog.Web.Mvc.Controllers"
						                                                         });
		}

		private static void MapSecurityRoutes(this RouteCollection routes)
		{
			routes.MapLowercaseRoute("Login", "login", new
				                                           {
					                                           controller = "Security",
					                                           action = "Login"
				                                           }, new[]
					                                              {
						                                              "CodeContribBlog.Web.Mvc.Controllers"
					                                              });

			routes.MapLowercaseRoute("Logout", "logout", new
				                                             {
					                                             controller = "Security",
					                                             action = "Logout"
				                                             }, new[]
					                                                {
						                                                "CodeContribBlog.Web.Mvc.Controllers"
					                                                });
		}

		public static void MapSyndicationRoutes(this RouteCollection routes)
		{
			routes.MapTenantRoute("CommentRss", "{tenant}/commentrss/{tag}", new
				                                                                 {
					                                                                 tenant = "default",
					                                                                 controller = "Syndication",
					                                                                 action = "CommentRss",
					                                                                 tag = UrlParameter.Optional
				                                                                 }, new
					                                                                    {
						                                                                    tenant = new TenantRouteConstraint()
					                                                                    }, new[]
						                                                                       {
							                                                                       "CodeContribBlog.Web.Mvc.Controllers"
						                                                                       });

			routes.MapTenantRoute("Rsd", "{tenant}/rsd", new
				                                             {
					                                             tenant = "default",
					                                             controller = "Syndication",
					                                             action = "Rsd"
				                                             }, new
					                                                {
						                                                tenant = new TenantRouteConstraint()
					                                                }, new[]
						                                                   {
							                                                   "CodeContribBlog.Web.Mvc.Controllers"
						                                                   });

			routes.MapTenantRoute("Rss", "{tenant}/rss/{tag}", new
				                                                   {
					                                                   tenant = "default",
					                                                   controller = "Syndication",
					                                                   action = "Rss",
					                                                   tag = UrlParameter.Optional
				                                                   }, new
					                                                      {
						                                                      tenant = new TenantRouteConstraint()
					                                                      }, new[]
						                                                         {
							                                                         "CodeContribBlog.Web.Mvc.Controllers"
						                                                         });
		}

		public static void RegisterRoutes(RouteCollection routes)
		{
			routes.MapSecurityRoutes();
			routes.MapSyndicationRoutes();
			routes.MapPostRoutes();
			routes.MapSearchRoutes();
			//routes.MapTenantPagingAndSortingRoutes("Post", "List", new[] { "CodeContribBlog.Web.Mvc.Controllers" });

			routes.MapTenantRoute("BlogHome", "{tenant}", new
				                                              {
					                                              controller = "Post",
					                                              action = "List"
					                                              //page = 1,
					                                              //size = PagingExtensions.PageSizes.FirstOrDefault(10)
				                                              }, new
					                                                 {
						                                                 tenant = new TenantRouteConstraint()
					                                                 }, new[]
						                                                    {
							                                                    "CodeContribBlog.Web.Mvc.Controllers"
						                                                    });

			routes.MapTenantRoute("BlogDefault", "{tenant}/{controller}/{action}/{id}", new
				                                                                            {
					                                                                            tenant = "default",
					                                                                            controller = "Post",
					                                                                            action = "List",
					                                                                            id = UrlParameter.Optional
				                                                                            }, new
					                                                                               {
						                                                                               id = new PositiveIntegerRouteConstraint(),
						                                                                               tenant = new TenantRouteConstraint()
					                                                                               }, new[]
						                                                                                  {
							                                                                                  "CodeContribBlog.Web.Mvc.Controllers"
						                                                                                  });

			routes.MapLowercaseRoute("Home", "", new
				                                     {
					                                     controller = "Home",
					                                     action = "Index"
				                                     }, new[]
					                                        {
						                                        "CodeContribBlog.Web.Mvc.Controllers"
					                                        });

			routes.MapLowercaseRoute("Default", "{controller}/{action}/{id}", new
				                                                                  {
					                                                                  controller = "Home",
					                                                                  action = "Index",
					                                                                  id = UrlParameter.Optional
				                                                                  }, new
					                                                                     {
						                                                                     id = new PositiveIntegerRouteConstraint()
					                                                                     }, new[]
						                                                                        {
							                                                                        "CodeContribBlog.Web.Mvc.Controllers"
						                                                                        });
		}
	}
}